#include "aky_program/aky_com_include.h"
static void my_event_cb(lv_obj_t * obj, lv_event_t event);
void my_ui3(void);
void notice_widow_set_stat(uint8_t stat);


lv_obj_t *obj_notice;
lv_obj_t *img_notice;
static lv_obj_t *tc_area_back;          //
static lv_obj_t *label_notice;          //
static lv_obj_t *tc_area_start_train;          //开始此模式训练
static lv_obj_t *tc_area_back_sub;          //子返回

lv_obj_t *img_bg_trans;

static lv_obj_t *label_scheme_1;          //方案一
static lv_obj_t *label_scheme_2;          //
static lv_obj_t *label_scheme_3;          
static lv_obj_t *label_scheme_4;          //


uint8_t flag_choose_scheme = 0;
uint8_t pre_adapt_mode_sel = 0;
uint8_t flag_manual_pre_adapt = 0;
uint8_t flag_tc_area_start_train_event_trig = 0;


/*
方案二: 训练仪内固定训练模式,  数据不可调整, 训\n练压力为180mmHg, 压迫5min,休息5min,共计5个\n循环，休息5分钟后测量双臂血压
 */

/*
 * 界面回调函数
 */
static void screen010_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    static uint16_t time_cnt = 0;
    screen_flush_pro();
    if (get_spk_recogni_info() == SPK_BACK)
    {
        clr_spk_recogni_info();
        notice_widow_set_stat(0);
        stat_bar_restart_flush();
        flag_choose_scheme = 0;
        synwit_ui_load_screen(SCREEN005);    
    }
    if (flag_tc_area_start_train_event_trig == 1)
    {
        time_cnt++;
        if (time_cnt > 100 / 10)
        {
            time_cnt = 0;
            flag_tc_area_start_train_event_trig = 0;
        }
    }
    else
    {
        time_cnt = 0;
    }
    if (get_spk_recogni_info() == START_THIS_TRAIN)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_start_train,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == PLAN_1_TRAINING)
    {
        clr_spk_recogni_info();
        flag_choose_scheme = 0;
        lv_event_send(label_scheme_1,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == PLAN_2_TRAINING)
    {
        clr_spk_recogni_info();
        flag_choose_scheme = 0;
        lv_event_send(label_scheme_2,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == PLAN_3_TRAINING)
    {
        clr_spk_recogni_info();
        flag_choose_scheme = 0;
        lv_event_send(label_scheme_3,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == PLAN_4_TRAINING)
    {
        clr_spk_recogni_info();
        flag_choose_scheme = 0;
        lv_event_send(label_scheme_4,LV_EVENT_CLICKED,NULL);
    }
}

static void screen010_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    flag_choose_scheme = 0;
    pre_adapt_mode_sel = 0;
    flag_manual_pre_adapt = 0;
    flag_tc_area_start_train_event_trig = 0;

    tc_area_back = synwit_ui_find_lv_obj(WIDGET_TOUCH_28);
    label_notice = synwit_ui_find_lv_obj(WIDGET_LABEL2);
    tc_area_start_train = synwit_ui_find_lv_obj(WIDGET_TOUCH_26);
    tc_area_back_sub = synwit_ui_find_lv_obj(WIDGET_TOUCH_27);

    label_scheme_1 = synwit_ui_find_lv_obj(WIDGET_LABEL41);
    label_scheme_2 = synwit_ui_find_lv_obj(WIDGET_LABEL42);
    label_scheme_3 = synwit_ui_find_lv_obj(WIDGET_LABEL43);
    label_scheme_4 = synwit_ui_find_lv_obj(WIDGET_LABEL44);

    img_bg_trans = synwit_ui_find_lv_obj(WIDGET_IMAGE17);

    lv_obj_set_click(label_scheme_1, true);
    lv_obj_set_click(label_scheme_2, true);
    lv_obj_set_click(label_scheme_3, true);
    lv_obj_set_click(label_scheme_4, true);
    lv_obj_set_click(img_bg_trans, true);


    lv_obj_set_hidden(label_notice, true);
    lv_obj_set_hidden(img_bg_trans, true);

    lv_obj_set_event_cb(tc_area_back, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_start_train, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_back_sub, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_scheme_1, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_scheme_2, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_scheme_3, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_scheme_4, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(img_bg_trans, my_event_cb);   /*Assign an event callback*/
    my_ui3();

    notice_widow_set_stat(0);
    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
   synwit_ui_start_scr_timer(10, NULL);
}

static void screen010_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();
}

static int screen010_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen010_cb_obj = {
    .favorite_stop_policy = screen010_favorite_stop_policy,
    .on_screen_start = screen010_start,
    .on_screen_stop = screen010_stop,
    .on_screen_timer = screen010_on_timer,
};

void my_ui3(void)
{
    static uint8_t flag_init_onece = 0;
    if (flag_init_onece == 0)
    {
        flag_init_onece = 1;
    }
    else
    {
        return;
    }

    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, LV_STATE_DEFAULT, 0);
    /*Make a gradient*/
    lv_style_set_bg_opa(&style, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_style_set_bg_color(&style, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_border_width(&style, LV_STATE_DEFAULT, 0);
    lv_style_set_outline_width(&style, LV_STATE_DEFAULT, 0);
    obj_notice = lv_obj_create(lv_scr_act(), NULL);
    lv_obj_set_pos(obj_notice, 0, 0);
    lv_obj_set_size(obj_notice, 770, 310);
    lv_obj_set_hidden(obj_notice, true);
    lv_obj_add_style(obj_notice, LV_OBJ_PART_MAIN, &style);
    lv_obj_set_click(obj_notice, false);
    lv_obj_set_drag(obj_notice, false);
    
    img_notice = lv_img_create(obj_notice, NULL);
    lv_img_set_src(img_notice,"S:spi:img/notice_window.bin");
    lv_obj_set_pos(img_notice, 0, 210);
    lv_obj_set_click(obj_notice, false);

    lv_obj_set_parent(tc_area_start_train,img_notice);
    lv_obj_set_parent(tc_area_back_sub,img_notice);
    
    lv_obj_set_pos(tc_area_start_train, 116, 16);
    lv_obj_set_pos(tc_area_back_sub, 510, 16);

    lv_obj_set_parent(label_notice,obj_notice);
    lv_obj_set_pos(label_notice, 25, 30);

}
#define LABEL_BG_COLOR_NORMAL (0Xe4e4e6)
#define LABEL_WORD_COLOR_NORMAL (0X092468)

#define LABEL_BG_COLOR_TRANS (0Xf0f0f2)
#define LABEL_WORD_COLOR_TRANS (0X7384a8) 

void notice_widow_set_stat(uint8_t stat)
{
    pre_adapt_mode_sel = stat;
    if (stat == 0)
    {
        lv_obj_set_click(tc_area_back, true);
        lv_obj_set_click(label_scheme_1, true);
        lv_obj_set_click(label_scheme_2, true);
        lv_obj_set_click(label_scheme_3, true);
        lv_obj_set_click(label_scheme_4, true);
        lv_obj_set_click(img_bg_trans, false);    
    }
    else
    {
        lv_obj_set_click(tc_area_back, false);
        lv_obj_set_click(label_scheme_1, false);
        lv_obj_set_click(label_scheme_2, false);
        lv_obj_set_click(label_scheme_3, false);
        lv_obj_set_click(label_scheme_4, false);
        lv_obj_set_click(img_bg_trans, true);
    }

    if (stat == 0)
    {
        lv_obj_set_hidden(obj_notice, true);
        lv_obj_set_size(obj_notice, 770, 310);
        lv_obj_set_pos(img_notice, 0, 210);
        lv_obj_set_hidden(img_bg_trans, true);

        lv_obj_set_style_local_bg_color(label_scheme_1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_1,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));

        lv_obj_set_style_local_bg_color(label_scheme_2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_2,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));

        lv_obj_set_style_local_bg_color(label_scheme_3, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_3,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));
        
        lv_obj_set_style_local_bg_color(label_scheme_4, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_4,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));

    }
    else if (stat == 1)
    {
        //方案一
        lv_obj_set_hidden(label_notice, false);
        lv_obj_set_pos(obj_notice, 125, 240);
        lv_obj_set_hidden(obj_notice, false);
        lv_label_set_text_fmt(label_notice, "方案一: 训练仪内固定训练模式,  数据不可调整, 训\n练压力为180mmHg, 压迫3min,休息3min,共计5\n个循环. 休息5分钟后测量双臂血压.");
        lv_obj_set_size(obj_notice, 770, 310);
        lv_obj_set_pos(img_notice, 0, 210);
        lv_obj_set_hidden(img_bg_trans, false);

        lv_obj_set_style_local_bg_color(label_scheme_1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_1,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));

        lv_obj_set_style_local_bg_color(label_scheme_2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_2,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

        lv_obj_set_style_local_bg_color(label_scheme_3, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_3,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));
        
        lv_obj_set_style_local_bg_color(label_scheme_4, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_4,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

    }
    else if (stat == 2)
    {
        //方案二
        lv_obj_set_hidden(label_notice, false);
        lv_obj_set_pos(obj_notice, 125, 240);
        lv_obj_set_hidden(obj_notice, false);
        lv_label_set_text_fmt(label_notice, "方案二: 训练仪内固定训练模式,  数据不可调整, 训\n练压力为180mmHg, 压迫5min,休息5min,共计5\n个循环. 休息5分钟后测量双臂血压.");
        lv_obj_set_size(obj_notice, 770, 310);
        lv_obj_set_pos(img_notice, 0, 210);
        lv_obj_set_hidden(img_bg_trans, false);

        lv_obj_set_style_local_bg_color(label_scheme_1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_1,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

        lv_obj_set_style_local_bg_color(label_scheme_2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_2,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));

        lv_obj_set_style_local_bg_color(label_scheme_3, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_3,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));
        
        lv_obj_set_style_local_bg_color(label_scheme_4, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_4,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

    }
    else if (stat == 3)
    {
        //方案三
        lv_obj_set_hidden(label_notice, false);
        lv_obj_set_pos(obj_notice, 220, 23);
        lv_obj_set_hidden(obj_notice, false);
        lv_label_set_text_fmt(label_notice, "方案三: 训练仪内固定训练模式,  数据不可调整, 训\n练压力为200mmHg, 压迫5min,休息5min,共计5\n个循环. 休息5分钟后测量双臂血压.");
        lv_obj_set_size(obj_notice, 770, 290);
        lv_obj_set_pos(img_notice, 0, 190);
        lv_obj_set_hidden(img_bg_trans, false);

        lv_obj_set_style_local_bg_color(label_scheme_1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_1,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

        lv_obj_set_style_local_bg_color(label_scheme_2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_2,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

        lv_obj_set_style_local_bg_color(label_scheme_3, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_3,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));
        
        lv_obj_set_style_local_bg_color(label_scheme_4, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_4,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));
        stat_bar_stop_flush(1);
    }
    else if (stat == 4)
    {
        //方案三
        lv_obj_set_hidden(label_notice, false);
        lv_obj_set_pos(obj_notice, 50, 23);
        lv_obj_set_hidden(obj_notice, false);
        lv_label_set_text_fmt(label_notice, "方案四: 训练仪内固定训练模式,  数据不可调整, 训\n练压力为220mmHg, 压迫5min,休息5min,共计5\n个循环. 休息5分钟后测量双臂血压.");
        lv_obj_set_size(obj_notice, 770, 290);
        lv_obj_set_pos(img_notice, 0, 190);
        lv_obj_set_hidden(img_bg_trans, false);

        lv_obj_set_style_local_bg_color(label_scheme_1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_1,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

        lv_obj_set_style_local_bg_color(label_scheme_2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_2,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));

        lv_obj_set_style_local_bg_color(label_scheme_3, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_TRANS));
        lv_obj_set_style_local_text_color(label_scheme_3,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_TRANS));
        
        lv_obj_set_style_local_bg_color(label_scheme_4, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_BG_COLOR_NORMAL));
        lv_obj_set_style_local_text_color(label_scheme_4,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(LABEL_WORD_COLOR_NORMAL));
        stat_bar_stop_flush(2);
        lv_obj_move_foreground(obj_notice);
    }
}

static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (flag_choose_scheme == 1)
    {
        if (obj == tc_area_start_train)
        {
            printf("tc_area_start_train event = %d\n",event);
            flag_tc_area_start_train_event_trig = 1;
            if (event == LV_EVENT_CLICKED)
            {
                flag_manual_pre_adapt = 1;
                stat_bar_restart_flush();
                synwit_ui_load_screen(SCREEN008);
            }
        }    
        if (obj == tc_area_back_sub)
        {
            printf("tc_area_back_sub event = %d\n",event);
            if (event == LV_EVENT_CLICKED)
            {
                notice_widow_set_stat(0);
                stat_bar_restart_flush();
                flag_choose_scheme = 0;
                // synwit_ui_load_screen(SCREEN005);
            }
        }
        if (obj == img_bg_trans)
        {
            printf("img_bg_trans event = %d\n",event);
            if (event == LV_EVENT_CLICKED)
            {
                if (flag_tc_area_start_train_event_trig == 0)
                {
                    notice_widow_set_stat(0);
                    stat_bar_restart_flush();
                    flag_choose_scheme = 0;
                }
                // synwit_ui_load_screen(SCREEN005);
            }
        }        
        return;
    }

    if (obj == tc_area_back)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN005);
        }
    }
    if (obj == label_scheme_1)
    {
        if (event == LV_EVENT_CLICKED)
        {
            flag_choose_scheme = 1;
            notice_widow_set_stat(1);
        }
    }
    if (obj == label_scheme_2)
    {
        if (event == LV_EVENT_CLICKED)
        {
            flag_choose_scheme = 1;
            notice_widow_set_stat(2);
        }
    }
    if (obj == label_scheme_3)
    {
        if (event == LV_EVENT_CLICKED)
        {
            flag_choose_scheme = 1;
            notice_widow_set_stat(3);
        }
    }
    if (obj == label_scheme_4)
    {
        if (event == LV_EVENT_CLICKED)
        {
            flag_choose_scheme = 1;
            notice_widow_set_stat(4);
        }
    }
}

